package demo1;

import java.util.Arrays;

public class MyStack {
    private int[] elem;
    private int usedSize;

    public static final int DEFAULT_CAPACITY = 10;

    public MyStack() {
        this.elem = new int[DEFAULT_CAPACITY];
    }


    //增加值
    public void push(int val) {
        if (isFull(elem)) {
            elem = Arrays.copyOf(elem, 2 * elem.length); //扩容
        } else {
            elem[usedSize] = val;
            usedSize++;
        }
    }


    //判断是否满了
    public boolean isFull(int[] elem) {
        return usedSize == elem.length;
    }


    //删除元素
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException();
        }
        int oldVal = elem[usedSize - 1];
        usedSize--;
        return oldVal;

    }


    //
    public int peek() {
        if (isEmpty()) {
            throw new RuntimeException();
        }
        return elem[usedSize - 1];
    }


    //
    private boolean isEmpty() {
        return usedSize == 0;
    }


}


